package com.skwyb.form.controller.system;

import com.skwyb.form.bean.dto.SysDictEntry;
import com.skwyb.form.bean.req.IdIntReq;
import com.skwyb.form.bean.req.IdReq;
import com.skwyb.form.core.BusinessException;
import com.skwyb.form.core.ObjectResult;
import com.skwyb.form.service.system.DictEntryService;
import org.hibernate.validator.constraints.Length;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;

@RestController
@RequestMapping("/admin/dict")
public class DictEntryController {

    @Autowired
    private DictEntryService dictService;

    @GetMapping("/list")
    public ObjectResult list(@Length(max = 20, message = "typeId too long") String typeId) {
        return new ObjectResult(dictService.selectByType(typeId));
    }

    @PostMapping("/get")
    public ObjectResult get(@Valid IdReq req) {
        return new ObjectResult(dictService.getById(req.getId()));
    }

    @PostMapping("/save")
    public ObjectResult save(@Valid SysDictEntry record) throws BusinessException {
        return new ObjectResult(dictService.saveDict(record));
    }

    @PostMapping("/delete")
    public ObjectResult delete(@Valid IdIntReq req) throws BusinessException {
        return new ObjectResult(dictService.removeById(req.getId()));
    }
}
